﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.SysManage;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.SysManage
{
    public class UserManageService : IUserManageService
    {
        private readonly IRepository<UserManage> _studentService;

        public UserManageService(IRepository<UserManage> studentService)
        {
            _studentService = studentService;
        }

        public async Task<Response<List<UserManage>>> GetUserManageAsync(UserManageForm form)
        {
            var students = await _studentService.GetAllListAsync();
            if(form.departId != 0)
            {
                students = students.Where(x => x.DepartId == form.departId).ToList();
            }
            if (!string.IsNullOrEmpty(form.personName))
            {
                students = students.Where(x => x.PersonName.Contains(form.personName)).ToList();
            }
            var count = students.Count();
            students = students.Skip((form.pageIndex - 1) * form.pageSize).Take(form.pageSize).ToList();
            var result = students.MapToList<UserManage>();
            return Response<List<UserManage>>.Ok(result, count);
        }

        public async Task<Response<bool>> PutUpdateUserManageFalse(UserManage input)
        {
            input.State = false;
            var result = _studentService.UpdateAsync(input.MapTo<UserManage>());

            return Response<bool>.Ok(result.Result > 0);
        }
    }
}
